package gov.kqqzyyy.monkey.radiologyscreen.ui;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.radiologyscreen.R;
import gov.kqqzyyy.monkey.radiologyscreen.adapter.MyPatientAdapter;
import gov.kqqzyyy.monkey.radiologyscreen.mytools.MyBaseActivity;
import gov.kqqzyyy.monkey.radiologyscreen.mytools.MyServiceUtil;
import gov.kqqzyyy.monkey.radiologyscreen.object.FSK_Patients;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends MyBaseActivity {

    @BindView(R.id.window_name_text)
    TextView mWindowText;
    @BindView(R.id.datetime_text)
    TextView mTimeText;
    @BindView(R.id.version_text)
    TextView mVersionText;
    @BindView(R.id.patient_listview)
    ListView mListView;

    private MyPatientAdapter adapter;

    private Call<String> mPatientsCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        adapter = new MyPatientAdapter(this);
        mListView.setAdapter(adapter);

        setTimeTextView(mTimeText);
        setVersionTextView(mVersionText);
        setRollListView(mListView);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mWindowText.setText("放射科");
        doFakeData();
    }


    private void doFakeData() {

        List<FSK_Patients> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            FSK_Patients patients = new FSK_Patients();
            patients.setPatientName("姓名" + i);
            String type = i % 2 == 0 ? "CT" : "DR";
            patients.setExamineType(type);

            int radMin = new Random().nextInt(239) + 1;
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE) - radMin, calendar.get(Calendar.SECOND));
            Date aTime = calendar.getTime();
            String saTime = getMyApplication().getSdfDateTime().format(aTime);
            patients.setAuditTime(saTime);
            list.add(patients);
        }
        doFilterData(list);

    }


    @Override
    protected void onStop() {
        super.onStop();
        if (mPatientsCall != null && mPatientsCall.isExecuted()) {
            mPatientsCall.cancel();
        }
    }

    @Override
    public void doFlushData() {
        super.doFlushData();
//        doGetPatients();
    }

    private void doGetPatients() {
        mPatientsCall = MyServiceUtil.getFSKPatients();
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    List<FSK_Patients> resultList = gson.fromJson(result, new TypeToken<List<FSK_Patients>>() {
                    }.getType());
                    doFilterData(resultList);
                } else {
                    Toast.makeText(getApplicationContext(), "获取病人数据失败：接口返回null", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "获取病人数据失败：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        };
        mPatientsCall.enqueue(callback);
    }

    private void doFilterData(List<FSK_Patients> list) {
        List<FSK_Patients> mPatientList = new ArrayList<>();
        if (list != null) {
            for (FSK_Patients patient : list
                    ) {
                if (isShowPatient(patient.getAuditTime())) {
                    mPatientList.add(patient);
                }
            }
        }
        adapter.setDataList(mPatientList);
    }

    private boolean isShowPatient(String auditTime) {
        Calendar calendar = Calendar.getInstance();
        try {
//            Date rightDate = getMyApplication().getSdfDateTime().parse(getShowDate());
            Date rightDate = new Date();
            calendar.setTime(rightDate);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE) - 3, calendar.get(Calendar.SECOND));
            Date endTime = calendar.getTime();
            calendar.setTime(rightDate);
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY) - 2, calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
            Date startTime = calendar.getTime();
            Date wcdate = getMyApplication().getSdfDateTime().parse(auditTime);
            return wcdate.before(endTime) && wcdate.after(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }


}
